﻿
using System;
using System.Data;
using System.Collections.Generic;
namespace ZHDJ.MobileInterface
{
    public partial class UserLogin : AbstractBasePage
    {
        protected override OutParam BusinessProcess<InParam, OutParam>(InParam inParam)
        {  
            // 业务逻辑返回的响应对象  
            LoginOutParam result = new LoginOutParam();
            LoginInParam param = new LoginInParam();
            string logMsg = "";
            string currentDay = "";
            bool isFlag = true;

            try
            {
                param = inParam as LoginInParam;
                if (string.IsNullOrEmpty(param.login_name) || string.IsNullOrEmpty(param.pwd))
                {
                    result.code = "0";
                    result.desc = "用户名/身份证/手机号码，密码不能为空";

                    isFlag = false;
                }

                if (isFlag)
                {
                    if (CommonUtils.IsDisableUsers(param.login_name))
                    {
                        result.code = "0";
                        result.desc = "用户状态有问题，请联系管理员";

                        isFlag = false;
                    }
                }

                string loginID = "";
                if(isFlag)
                {
                    ///1 用户名;2 手机号码;3身份证;(为空时,默认为使用用户名)
                    //是否重复
                    //是否存在
                    DataSet dsMember;
                    if (param.type == "2")
                    {
                        dsMember = InterfaceHelper.GetMembersByFilter(" MobileNo='" + param.login_name + "' and  runSate<>'4' ");
                        if (dsMember == null || dsMember.Tables[0].Rows.Count == 0)
                        {
                            result.code = "0";
                            result.desc = "手机号码不存在";

                            isFlag = false;
                        }
                        else if (dsMember.Tables[0].Rows.Count > 1)
                        {
                            result.code = "0";
                            result.desc = "手机号码不唯一,请改用登陆帐号或身份证方式登陆";

                            isFlag = false;
                        } 
                        else
                        {
                            loginID = dsMember.Tables[0].Rows[0]["LoginID"].ToString();
                        }
                    }
                    else if (param.type == "3")
                    {
                        dsMember = InterfaceHelper.GetMembersByFilter(" IdentityCard='" + param.login_name + "' and  runSate<>'4' ");
                        if (dsMember == null || dsMember.Tables[0].Rows.Count == 0)
                        {
                            result.code = "0";
                            result.desc = "身份证号码不存在";

                            isFlag = false;
                        }
                        else if (dsMember.Tables[0].Rows.Count > 1)
                        {
                            result.code = "0";
                            result.desc = "身份证号码不唯一,请改用登陆帐号或手机号码方式登陆";

                            isFlag = false;
                        } 
                        else
                        {
                            loginID = dsMember.Tables[0].Rows[0]["LoginID"].ToString();
                        }
                    }
                    else
                    {
                        loginID = InterfaceHelper.GetLoginID(param.login_name);
                        if (string.IsNullOrEmpty(loginID))
                        {
                            result.code = "0";
                            result.desc = "登陆账号不存在";

                            isFlag = false;
                        } 
                    }
                }

                if (isFlag)
                {
                    //param.pwd = CommonUtils.GetMD5(param.pwd);
                    isFlag = InterfaceHelper.PasswordIsCorrect(loginID, param.pwd);

                    if (!isFlag)
                    {
                        result.code = "0";
                        result.desc = "用户名，密码输入不正确";
                    }
                }

                //检查登陆账号是否开通
                if (isFlag)
                {
                    if (!InterfaceHelper.LoginIDEnable(loginID))
                    { 
                        result.code = "0";
                        result.desc = "您的账号还没启用，请联系系统管理员";

                        isFlag = false;
                    }
                }

                if (isFlag)
                {
                    //增加登陆积分
                    //InterfaceHelper.ScoreManagePartyLoginAdd(loginID);

                    //增加登陆日志
                    //InterfaceHelper.InsertUserLoginLog(loginID);

                    DataSet ds = InterfaceHelper.GetMembersLoginInfo(loginID);
                    DateTime dtTemp;
                    Birthdays birthday = new Birthdays();
                    if (ds != null && ds.Tables[0].Rows.Count > 0)
                    {  
                        string urlPath = CommonUtils.AppAddressEx + "html/images";
                        result.data.branch_id = ds.Tables[0].Rows[0]["branch_id"].ToString(); 
                        result.data.user_id = ds.Tables[0].Rows[0]["user_id"].ToString();
                        result.data.user_name = ds.Tables[0].Rows[0]["user_name"].ToString();
                        result.data.user_type = ds.Tables[0].Rows[0]["user_type"].ToString(); 
                        if (Convert.ToDecimal(ds.Tables[0].Rows[0]["is_can_test"]) >= 3)
                            result.data.is_can_test = "2";
                        else
                            result.data.is_can_test = "1";

                        //是否生日
                        try
                        {
                            dtTemp = Convert.ToDateTime(ds.Tables[0].Rows[0]["Birthday"]);
                            if (dtTemp.Year != 9999 && dtTemp.Year != 1753 && dtTemp.Month == DateTime.Now.Month && dtTemp.Day == DateTime.Now.Day)
                            {
                                birthday = new Birthdays();
                                birthday.birthday_type = "2";
                                birthday.img_url = urlPath + "/personImage.png";
                                birthday.version = ServiceSettings.BirthdaysVersion;
                                result.data.birthdays.Add(birthday);
                            }
                        }
                        catch
                        {
                        }

                        //是否入党日
                        try
                        {
                            dtTemp = Convert.ToDateTime(ds.Tables[0].Rows[0]["JoinPartyDate"]);
                            if (dtTemp.Year != 9999 && dtTemp.Year != 1753 && dtTemp.Month == DateTime.Now.Month && dtTemp.Day == DateTime.Now.Day)
                            {
                                birthday = new Birthdays();
                                birthday.birthday_type = "3";
                                birthday.img_url = urlPath + "/joinImage.png";
                                birthday.version = ServiceSettings.BirthdaysVersion;
                                result.data.birthdays.Add(birthday);
                            }
                        }
                        catch
                        {
                        }

                        //是否党的生日
                        currentDay = (DateTime.Now.Month * 100 + DateTime.Now.Day).ToString().PadLeft(4, '0');
                        if (ServiceSettings.PartyBirthday.IndexOf(currentDay) > 0)
                        {
                            birthday = new Birthdays();
                            birthday.birthday_type = "1";
                            birthday.img_url = urlPath + "/partyImage.png";
                            birthday.version = ServiceSettings.BirthdaysVersion;
                            result.data.birthdays.Add(birthday);
                        } 
                    } 
                }

                logMsg = result.desc;
            }
            catch (Exception ex)
            {
                result.code = "0";
                result.desc = "操作异常，多次出现请联系管理员";
                logMsg = ex.Message + ":" + ex.StackTrace;
            }

            string urlPara = Request.Url.AbsoluteUri;
            urlPara += ";总参数数目[" + Request.QueryString.Count.ToString() + "]";

            for (int i = 0; i < Request.QueryString.Count; i++)
            {
                urlPara += ":" + Request.QueryString[i];
            }
            CommonUtils.InsertMobileInterfaceLog(SystemCode + "-UserLogin", SerializeHelper.JsonSerializer<LoginInParam>(param), urlPara + ";" + logMsg + currentDay);
            return result as OutParam;
        }

        protected void Page_Load(object sender, System.EventArgs e)
        {
            if (string.Compare(Request.QueryString["Safe"], "False", true) == 0)
                ProcessRequest<LoginInParam, LoginOutParam>();
            else
                SafeProcessRequest<LoginInParam, LoginOutParam>();
        }
    }

    public class LoginInParam
    {
        /// <summary>
        /// 登陆名 必须，英文名或者身份证号码登陆(面前网站没有对身份证号码进行唯一性验证，先只使用用户名验证)
        /// </summary>
        public string login_name
        { get; set; }

        public string pwd
        { get; set; }

        /// <summary>
        ///	登陆类型 1 用户名;2 手机号码;3身份证;(为空时,默认为使用用户名) 
        /// </summary>
        public string type
        { get; set; }
    }

    public class LoginOutParam
    {
        public LoginOutParam()
        {
            code = "1";
            desc = "操作成功";
            data = new LoginOutData();
        }

        public string code
        { get; set; }

        public string desc
        { get; set; }

        public LoginOutData data
        { get; set; }
    }

    public class LoginOutData
    {
        public LoginOutData()
        {
            birthdays = new List<Birthdays>();
        }

        public string user_id
        { get; set; }

        public string user_name
        { get; set; }

        public string branch_id
        { get; set; }

        /// <summary>
        /// 账号类型，即用户角色 1：党员账号、2：群众账号
        /// </summary>
        public string user_type
        { get; set; }

        /// <summary>
        /// 是否可参加今天的考试 1：可以，2：不可以
        /// </summary>
        public string is_can_test
        { get; set; }

        public List<Birthdays> birthdays
        {
            get;
            set;
        }

        ///// <summary>
        ///// 党生日图片地址
        ///// </summary>
        //public string party_url
        //{ get; set; }

        ///// <summary>
        ///// 个人生日图片地址
        ///// </summary>
        //public string person_url
        //{ get; set; }

        ///// <summary>
        ///// 入党生日图片地址
        ///// </summary>
        //public string join_url
        //{ get; set; }
    }

    public class Birthdays
    { 
        /// <summary>
        ///	生日类型 1：党生日，2：个人生日，3：入党生日 
        /// </summary>
        public string birthday_type
        { get; set; }

        /// <summary>
        ///	生日图片下载地址 
        /// </summary>
        public string img_url
        { get; set; }

        /// <summary>
        ///	最新版本号 
        /// </summary>
        public string version
        { get; set; }
    }
}